package com.blueocean_health.care.management.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.vo.ModuleVo;
import com.blueocean_health.care.management.em.ProjectTypeEnum;
import com.blueocean_health.care.management.mapper.ModuleMapper;
import com.blueocean_health.care.management.service.ModuleService;

/**
* 模块service 
* @author huyanqiu
* @date 2018年3月23日 下午2:38:32
* @version 版本号：1.0
*/
@Service
public class ModuleServiceImpl implements ModuleService{
	
	@Autowired
	private ModuleMapper moduleMapper;

	/**
	 * 获得模块清单，指定组织有权限使用的模块 
	 * @param type 类型：1.泰心端模块  2. 项目端模块
	 */
	@Override
	public SysResult list(Integer type, Integer orgId) {
		List<ModuleVo> resultModuleList = new ArrayList<>();
		List<ModuleVo> moduleList = new ArrayList<>();
		if (type==1) {
			moduleList = moduleMapper.findByOrgId(orgId);
			return SysResult.ok(moduleList);
		} else {
			
			// 获得所有运营端项目模块
			moduleList = moduleMapper.findAllProjectModule();
			ModuleVo appModule = new ModuleVo();
			appModule.setName(ProjectTypeEnum._1.getVal());
			appModule.setValue(ProjectTypeEnum._1.getKey());
			ModuleVo projectModule = new ModuleVo();
			projectModule.setName(ProjectTypeEnum._2.getVal());
			projectModule.setValue(ProjectTypeEnum._2.getKey());
			
			List<ModuleVo> appList = new ArrayList<>();
			List<ModuleVo> projectList = new ArrayList<>();
			System.out.println("moduleList:"+moduleList+"   :"+moduleList.isEmpty());
			
			for (ModuleVo moduleVo : moduleList) {
				if (Integer.parseInt(moduleVo.getValue())<200) {
					// 项目端
					projectList.add(moduleVo);
				} else {
					// app端
					appList.add(moduleVo);
				}
			}
			if (!appList.isEmpty()) {
				appModule.setItems(appList);
				resultModuleList.add(appModule);
			}
			if (!projectList.isEmpty()) {
				projectModule.setItems(projectList);
				resultModuleList.add(projectModule);
			}
		}
		return SysResult.ok(resultModuleList);
	}

}
